<?php
namespace App\Http\Controllers\Business\Admin\Log;

use App\Http\Controllers\Business\Shop\V1\Admin\Admin;
use App\Models\AdminUser;
use App\Models\City;
use App\Models\Shop;
use App\Models\ShopCheck;
use App\Models\ShopContract;
use Auth;
use Exception;
use App\Models\Province;

class ShopLog {
    //内容：商家代码、商家名称、商家状态、商务人员、最新操作时间 、操作类型, 来源
    //类型：提交审核、审核不通过、上架、取消今日翻倍花、开通今日翻倍花、下架、续签、修改维护人、修改收款帐号、修改商家登录号码、资料更新、修改翻倍日、删除，来源（商家版，后台）
    /*
     * 类型：
          'new':首次上线
          'downshelf':下架
          'upshelf':上架
           'cancel_doubleday': 取消今天翻倍日
           'open_doubleday':开通今天翻倍日
           'customer_check_pass': 客服审核通过
           'update_shop':修改商家信息
           'update_shop_maintainer':修改维护人
           'update_shop_oldmaintainer':修改原系统维护人
           'update_shop_mobile': 修改商家登录账号
           'update_shop_contract': 修改商家续约
           'update_shop_account': 修改商家收款账号
            'shop_set_doublecalendar':(商家)修改商家翻贝日期
     * */

    //用户简单操作 写入商家日志（type 上下架）
    public static function writeShopLog($shopId, $type, $withholdType, $reason, $remark) {
        if(!$shopId||!$reason) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $adminUser = Auth::guard('adminweb')->user();
        $contractorId = ShopContract::where('shopid', $shopId)->orderBy('id', 'desc')->pluck('contractorid')->first();
        if($contractorId) {
            $contractorName = AdminUser::where('id', $contractorId)->pluck('realname')->first();
        } else {
            $contractorId = 0;
            $contractorName = '';
        }
        $maintainerId = Shop::where('id', $shopId)->orderBy('id', 'desc')->pluck('maintainerid')->first();
        if($maintainerId) {
            $maintainerName = AdminUser::where('id', $maintainerId)->pluck('realname')->first();
        } else {
            $maintainerId = 0;
            $maintainerName = '';
        }
        $shopCheck = ShopCheck::create([
            'shopid' => $shopId,
            'type' => $type,
            'withholdtype' => $withholdType,
            'reason' => $reason,
            'remark' => $remark,
            'contractorid' => $contractorId,
            'contractorname' => $contractorName,
            'maintainerid' => $maintainerId,
            'maintainername' => $maintainerName,
            'adminid' => $adminUser->id,
        ]);
        $shopCheck->save();
        return ['status'=>'success', 'message'=>'写入成功'];
    }
    //商家资料更新，
    public static function writeLogAfterShopInfoUpdated($shopId,$type,$data,$shopInfo,$event = '',$shopEvent=[]) {
        $reason = '';
        if(!empty($data)){
            if($type == 'basic'){
                if(strcmp($shopInfo['name'],$data['name']) != '0'){
                    $reason .= '门店名称发生了改变;';
                }
                if(strcmp($shopInfo['code'],$data['code']) != '0'){
                    $reason .= '门店编号由"'.$shopInfo['code'].'"改成"'.$data['code'].'";';
                }
                if(strcmp($shopInfo['contactnumber'],$data['contactnumber']) != '0'){
                    $reason .= '前台电话由"'.$shopInfo['contactnumber'].'"改成"'.$data['contactnumber'].'";';
                }
                if(strcmp($shopInfo['businesshours'],$data['businesshours']) != '0'){
                    $reason .= '营业时间由"'.$shopInfo['businesshours'].'"改成"'.$data['businesshours'].'";';
                }
                if(strcmp($shopInfo['smsphone'],$data['smsphone']) != '0'){
                    $reason .= '接收短信手机由"'.$shopInfo['smsphone'].'"改成"'.$data['smsphone'].'";';
                }
                if($shopInfo['provinceid'] != $data['provinceid']){
                    $prinvore = Province::lists('name','id');
                    $reason .= '门店所在省由"'.$prinvore[$shopInfo['cityname']].'"改成"'.$prinvore[$data['cityname']].'";';
                }
                if(strcmp($shopInfo['code'],$data['code']) != '0'){
                    $reason .= '门店所在市由"'.$shopInfo['cityname'].'"改成"'.$data['cityname'].'";';
                }
                if(strcmp($shopInfo['districtname'],$data['districtname']) != '0'){
                    $reason .= '门店所在区由"'.$shopInfo['districtname'].'"改成"'.$data['districtname'].'";';
                }
                if(strcmp($shopInfo['businessareaname'],$data['businessareaname']) != '0'){
                    $reason .= '门店所在商圈由"'.$shopInfo['businessareaname'].'"改成"'.$data['businessareaname'].'";';
                }
                if(strcmp($shopInfo['address'],$data['address']) != '0'){
                    $reason .= '门店具体地址发生改变;';
                }
                if(strcmp($shopInfo['firstclassname'],$data['firstclassname']) != '0'){
                    $reason .= '经营一级分类由"'.$shopInfo['firstclassname'].'"改成"'.$data['firstclassname'].'";';
                }
                if(strcmp($shopInfo['secondclassname'],$data['secondclassname']) != '0'){
                    $reason .= '经营二级分类由"'.$shopInfo['secondclassname'].'"改成"'.$data['secondclassname'].'";';
                }
                if(strcmp($shopInfo['label'],$data['label']) != '0'){
                    $reason .= '商家标签由"'.$shopInfo['label'].'"改成"'.$data['label'].'";';
                }
                if(strcmp($shopInfo['description'],$data['description']) != '0'){
                    $reason .= '商家简介发生改变;';
                }
            }elseif($type == 'photo'){
                if(strcmp($shopInfo['logo'],$data['logo']) != '0'){
                    $reason .= '商家主图发生改变;';
                }
                if(strcmp($shopInfo['photos'],$data['photos']) != '0'){
                    $reason .= '商家环境图发生改变;';
                }
                if(strcmp($shopInfo['goodsphotos'],$data['goodsphotos']) != '0'){
                    $reason .= '商家商品信息发生改变;';
                }
            }elseif($type == 'doublecalendar'){
                if($shopInfo['weekdoubleday'] != $data['weekdoubleday']){
                    $reason .= '商家每周翻倍日上限天数由"'.$shopInfo['weekdoubleday'].'"改成"'.$data['weekdoubleday'].'";';
                }
                $shopEventStr = '';
                $eventArr = explode(',',$event);
                foreach($eventArr as $key=>$value){
                    $eventArrTrim[$key] = trim($value,' ');
                }
                $shopEventArr=[];
                foreach($shopEvent as $key=>$value){
                    $shopEventArr[$key] = $value['event'];
                    $shopEventStr .= $value['event'];
                }
                $eventDiff = array_merge(array_diff($eventArrTrim,$shopEventArr),array_diff($shopEventArr,$eventArrTrim));
                if(!empty($eventDiff)){
                    if(count($eventDiff) >20){
                        $reason .= '商家翻贝日期发生改变;';
                    }else{
                        $reason .= '商家翻贝日期更改了"'.implode(',',$eventDiff).'";';
                    }
                }
            }elseif($type == 'payrules'){
                if($shopInfo['buynumber'] != $data['buynumber']){
                    if($shopInfo['buynumber'] ==0){
                        $buynumber = '次数不限';
                    }else{
                        $buynumber= $shopInfo['buynumber'].'次/用户';
                    }
                    if($data['buynumber'] ==0){
                        $buynumberData = '次数不限';
                    }else{
                        $buynumberData= $data['buynumber'].'次/用户';
                    }
                    $reason .= '商家当日消费次数限制由"'.$buynumber.'"改成"'.$buynumberData.'";';
                }
                if($shopInfo['maxsales'] != $data['maxsales']){
                    if($shopInfo['maxsales'] ==0){
                        $maxsalesStr = '封顶金额不限';
                    }else{
                        $maxsalesStr= $shopInfo['maxsales'];
                    }
                    if($data['maxsales'] ==0){
                        $maxsalesStrData = '封顶金额不限';
                    }else{
                        $maxsalesStrData= $data['maxsales'];
                    }
                    $reason .= '商家封顶金额由"'.$maxsalesStr.'"改成"'.$maxsalesStrData.'";';
                }
                if(strcmp($shopInfo['notpartakegoods'],$data['notpartakegoods']) != '0'){
                    if($shopInfo['notpartakegoods'] ==''){
                        $maxsalesStr = '全场参与';
                    }else{
                        $maxsalesStr= $shopInfo['notpartakegoods'];
                    }
                    if($data['notpartakegoods'] ==''){
                        $maxsalesStrData = '全场参与';
                        $reason .= '商家全场参与由"全场参与,除'.$maxsalesStr.'不参与"改成"'.$maxsalesStrData.'";';
                    }else{
                        if($shopInfo['notpartakegoods'] == ''){
                            $maxsalesStrData= $data['notpartakegoods'];
                            $reason .= '商家全场参与由"'.$maxsalesStr.'"改成"全场参与,除'.$maxsalesStrData.'不参与";';
                        }else{
                            $maxsalesStrData= $data['notpartakegoods'];
                            $reason .= '商家全场参与由"全场参与,除'.$maxsalesStr.'不参与"改成"全场参与,除'.$maxsalesStrData.'不参与";';
                        }
                    }
                }
                if(strcmp($shopInfo['invoice'],$data['invoice']) != '0'){
                    $reason .= '发票由"'.$shopInfo['invoice'].'"改成"'.$data['invoice'].'";';
                }
                if(strcmp($shopInfo['content'],$data['content']) != '0'){
                    $reason .= '其他由"'.$shopInfo['content'].'"改成"'.$data['content'].'";';
                }
            }elseif($type == 'legalperson'){
                if(strcmp($shopInfo['parentname'],$data['parentname']) != '0'){
                    $reason .= '负责人姓名由"'.$shopInfo['parentname'].'"改成"'.$data['parentname'].'";';
                }
                if(strcmp($shopInfo['parentmobile'],$data['parentmobile']) != '0'){
                    $reason .= '商家登录帐号由"'.$shopInfo['parentmobile'].'"改成"'.$data['parentmobile'].'";';
                }
            }elseif($type =='qualification'){
                if(strcmp($shopInfo['bankcardphoto'],$data['bankcardphoto']) != '0'){
                    $reason .= '银行卡正面照发生了改变;';
                }
                if(strcmp($shopInfo['licensephoto'],$data['licensephoto']) != '0'){
                    $reason .= '上传营业执照发生了改变;';
                }
                if(strcmp($shopInfo['idcardfrontphoto'],$data['idcardfrontphoto']) != '0'){
                    $reason .= '身份证正面照发生了改变;';
                }
                if(strcmp($shopInfo['idcardbackphoto'],$data['idcardbackphoto']) != '0'){
                    $reason .= '身份证反面照由发生了改变;';
                }
            }
        }
        $reason = rtrim($reason, ";") ;
        $result = self::writeShopLog($shopId,'update_shop',0,$reason,'');
    }
}